package com.ccy.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccy.common.utils.PageUtils;
import com.ccy.common.utils.Query;
import com.ccy.product.dao.AttrDao;
import com.ccy.product.dao.AttrGroupDao;
import com.ccy.product.entity.AttrAttrgroupRelationEntity;
import com.ccy.product.entity.AttrEntity;
import com.ccy.product.entity.AttrGroupEntity;
import com.ccy.product.service.AttrAttrgroupRelationService;
import com.ccy.product.service.AttrGroupService;
import com.ccy.product.service.CategoryService;
import com.ccy.product.vo.AttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrDao attrDao;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        LambdaQueryWrapper<AttrGroupEntity> queryWrapper = Wrappers.<AttrGroupEntity>lambdaQuery()
                .eq(!Objects.equals(catelogId, 0L), AttrGroupEntity::getCatelogId, catelogId)
                .and(StringUtils.isNotBlank(key),wrapper -> wrapper.like(AttrGroupEntity::getAttrGroupName, key).or().like(AttrGroupEntity::getAttrGroupId, key));
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public AttrGroupEntity getInfo(Long attrGroupId) {
        AttrGroupEntity attrGroupEntity = this.getById(attrGroupId);
        List<Long> catlogPath = categoryService.findCatlogPath(attrGroupEntity.getCatelogId());
        attrGroupEntity.setCatelogPath(catlogPath);
        return attrGroupEntity;
    }

    @Override
    public List<AttrEntity> getAtrrRelations(Long attrgroupId) {
        return attrDao.getAtrrRelations(attrgroupId);
    }

    @Override
    public PageUtils getAtrrNotRelations(Map<String, Object> params, Long attrgroupId, Long catelogId) {
        String key = (String) params.get("key");
        String sidx = (String) params.get("sidx");
        String order = (String) params.get("order");
        String orderSql = String.format("order by %s %s", sidx, order);
        List<Long> attrIds = attrAttrgroupRelationService.list(Wrappers.<AttrAttrgroupRelationEntity>lambdaQuery()
                .eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupId))
                .stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        LambdaQueryWrapper<AttrEntity> queryWrapper = Wrappers.<AttrEntity>lambdaQuery()
                .eq(AttrEntity::getCatelogId, catelogId)
                .notIn(AttrEntity::getAttrId,attrIds)
                .and(StringUtils.isNotBlank(key), wrapper -> wrapper.like(AttrEntity::getAttrId, key).or().like(AttrEntity::getAttrName, key))
                .last(StringUtils.isNotBlank(sidx) && StringUtils.isNotBlank(order),orderSql);
        IPage<AttrEntity> page = attrDao.selectPage(new Query<AttrEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public void addAtrrRelations(List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntitys) {
        for (AttrAttrgroupRelationEntity attrAttrgroupRelationEntity : attrAttrgroupRelationEntitys) {
            attrAttrgroupRelationService.update(attrAttrgroupRelationEntity,Wrappers.<AttrAttrgroupRelationEntity>lambdaQuery()
                    .eq(AttrAttrgroupRelationEntity::getAttrId, attrAttrgroupRelationEntity.getAttrId()));
        }
    }

    @Override
    public void deleteAtrrRelations(List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntitys) {
        for (AttrAttrgroupRelationEntity attrAttrgroupRelationEntity : attrAttrgroupRelationEntitys) {
            Long attrId = attrAttrgroupRelationEntity.getAttrId();
            Long attrGroupId = attrAttrgroupRelationEntity.getAttrGroupId();
            attrAttrgroupRelationService.remove(Wrappers.<AttrAttrgroupRelationEntity>lambdaQuery()
                    .eq(AttrAttrgroupRelationEntity::getAttrId,attrId)
                    .eq(AttrAttrgroupRelationEntity::getAttrGroupId,attrGroupId));
        }
    }

    @Override
    public List<AttrGroupVo> getAllGroupWithAttrByCatelogId(Long catelogId) {
        return attrGroupDao.getAllGroupWithAttrByCatelogId(catelogId);
    }
}